﻿/* Write a method that adds two polynomials. 
 * Represent them as arrays of their coefficients as in the example below:
		x2 + 5 = 1x2 + 0x + 5  <5><0><1>
 */

using System;
using System.Globalization;
using System.Threading;

class Polynomials
{
    static void Main()
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

        Int32 dn;
        bool iProvided = false;

        //check for proper user input
        do {
            if (iProvided == true) Console.WriteLine("Error!: Invalid user input. Please enter a valid integer!\r\n\tValid value range: {0} - {1}", Int32.MinValue, Int32.MaxValue);
            else iProvided = true;
            Console.Write("Enter degree of your first polynomial: ");
        } while (Int32.TryParse(Console.ReadLine(), out dn) == false);

        string cmsg = "Enter coefficients starting from higest to smallest (from x powered by 2 to x powered by 0):";
        Console.WriteLine(cmsg);
        decimal[] arr_1 = new decimal[dn + 1];
        PutInStack_(arr_1);

        iProvided = false; //reset iProvided to avoid receiving a "false negative" error message
        do
        {
            if (iProvided == true) Console.WriteLine("Error!: Invalid user input. Please enter a valid integer!\r\n\tValid value range: {0} - {1}", Int32.MinValue, Int32.MaxValue);
            else iProvided = true;
            Console.Write("\r\nEnter degree of your second polynomial: ");
        } while (Int32.TryParse(Console.ReadLine(), out dn) == false);
        Console.WriteLine(cmsg);

        decimal[] arr_2 = new decimal[dn + 1];
        PutInStack_(arr_2);

        Console.Write("#1: ");
        Output_(arr_1);
        Console.Write("#2: ");
        Output_(arr_2);
        Console.Write("Sum: ");
        Output_(Sum_(arr_1, arr_2));
        Console.Write("Substracted: ");
        Output_(Substract(arr_1, arr_2));
        Console.Write("Multiplied: ");
        Output_(Multiply(arr_1, arr_2));
    }

    static void Output_(decimal[] arr)
    {
        for (int i = arr.Length - 1; i >= 0; i--)
        {
            if (arr[i] != 0)
            {
                if (i != 0 && i != 1)
                {
                    Console.Write(" {2}{0:F1}x^{1}", arr[i], i, ret_plus_sign(arr, i));
                }
                else if (i == 1)
                {
                    Console.Write(" {1}{0:F1}x", arr[i], ret_plus_sign(arr, i));

                }
                else if (i == 0)
                {
                    Console.Write(" {1}{0:F1}", arr[i], ret_plus_sign(arr, i));
                }
            }
        }
        Console.WriteLine();
    }

    static decimal[] Substract(decimal[] arr1, decimal[] arr2)
    {

        int biggerLength = arr1.Length;
        int smallerLength = arr2.Length;

        if (arr2.Length > arr1.Length)
        {
            biggerLength = arr2.Length;
            smallerLength = arr1.Length;
        }

        decimal[] sumArr = new decimal[biggerLength];

        for (int i = 0; i < biggerLength; i++)
        {
            if (i > smallerLength - 1)
            {
                sumArr[i] = arr1.Length > arr2.Length ? arr1[i] : arr2[i];
            }
            else
            {
                sumArr[i] = arr1[i] - arr2[i];
            }
        }
        return sumArr;
    }

    static decimal[] Multiply(decimal[] arr1, decimal[] arr2)
    {
        decimal[] multiArr = new decimal[arr1.Length + arr2.Length - 1];
        for (int i = 0; i < arr1.Length; i++)
        {
            for (int j = 0; j < arr2.Length; j++)
            {
                multiArr[i + j] += arr1[i] * arr2[j];
            }

        }
        return multiArr;
    }

    static decimal[] PutInStack_(decimal[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            Console.Write("Enter array element #{0}: ", i+1);
            decimal num = decimal.Parse(Console.ReadLine());
            arr[i] = num;
        }
        Array.Reverse(arr);
        return arr;
    }

    static string ret_plus_sign(decimal[] arr, int i)
    {
        if (arr[i] > 0 && arr.Length - 1 != i)
        {
            string signPlus = "+";
            return signPlus;
        }
        else
        {
            return null;
        }
    }
    static decimal[] Sum_(decimal[] arr1, decimal[] arr2)
    {

        int biggerLength = arr1.Length;
        int smallerLength = arr2.Length;

        if (arr2.Length > arr1.Length)
        {
            biggerLength = arr2.Length;
            smallerLength = arr1.Length;
        }

        decimal[] sum = new decimal[biggerLength];

        for (int i = 0; i < biggerLength; i++)
        {
            if (i > smallerLength - 1)
            {
                sum[i] = arr1.Length > arr2.Length ? arr1[i] : arr2[i];
            }
            else
            {
                sum[i] = arr1[i] + arr2[i];
            }

        }
        return sum;
    }
}
